﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cadcam7Moddel.Model;
using Cadcam7Moddel.Context;
using camcad7Home.CultureHelper;
namespace camcad7Home.Controllers
{
    public class CauhinhtrangController : CultureController
    {
        private DataContext db = new DataContext();

        //abc
        // GET: /Cauhinhtrang/
       public ActionResult jsonList()
        {

            string a = Request.Cookies["_culture"]["lang"];
            var list = from n in db.Cauhinhtrang
                       where n.LangID==a
                       orderby n.ID descending
                       select new {n.ID,n.Mota,n.TenCty,n.Sodt,n.Website,n.Email,n.banner,n.Diachi };
			
            return Json(list, JsonRequestBehavior.AllowGet);
        } 
		
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Cauhinhtrang/Details/5

        public ActionResult Details(int id = 0)
        {
            Cauhinhtrang cauhinhtrang = db.Cauhinhtrang.Find(id);
            if (cauhinhtrang == null)
            {
                return HttpNotFound();
            }
            return View(cauhinhtrang);
        }

        //
        // GET: /Cauhinhtrang/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Cauhinhtrang/Create

        [HttpPost]
        public ActionResult Create(Cauhinhtrang cauhinhtrang)
        {
            if (ModelState.IsValid)
            {
                string a = Request.Cookies["_culture"]["lang"];
                cauhinhtrang.LangID = a;
                db.Cauhinhtrang.Add(cauhinhtrang);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(cauhinhtrang);
        }

        //
        // GET: /Cauhinhtrang/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Cauhinhtrang cauhinhtrang = db.Cauhinhtrang.Find(id);
            if (cauhinhtrang == null)
            {
                return HttpNotFound();
            }
            return View(cauhinhtrang);
        }

        //
        // POST: /Cauhinhtrang/Edit/5

        [HttpPost]
        public ActionResult Edit(Cauhinhtrang cauhinhtrang)
        {
            if (ModelState.IsValid)
            {
                db.Entry(cauhinhtrang).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(cauhinhtrang);
        }

        //
        // GET: /Cauhinhtrang/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Cauhinhtrang cauhinhtrang = db.Cauhinhtrang.Find(id);
            if (cauhinhtrang == null)
            {
                return HttpNotFound();
            }
            return View(cauhinhtrang);
        }

        //
        // POST: /Cauhinhtrang/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Cauhinhtrang cauhinhtrang = db.Cauhinhtrang.Find(id);
            db.Cauhinhtrang.Remove(cauhinhtrang);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        public ActionResult  ajDelete(int id)
        {
            Cauhinhtrang cauhinh = db.Cauhinhtrang.Where(m => m.ID == id).FirstOrDefault();
            if (cauhinh != null)
            {
                db.Cauhinhtrang.Remove(cauhinh);
                //db.SaveChanges();
                db.SaveChanges();
                return Json(new { success = true });
            }
            else
            { 
                return Json(new { success = false });
            }
         }
    }
}